package com.JingYan.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author DongJiaQi
 * @create 2022/7/7 19:25
 * @Description: 实现mybatis-plus的自动填充需要实现MetaObjectHandler
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 要使用的类属性上加注释  参考 BaseDomain 类
     */
    //@TableField(value = "create_by"  ,fill = FieldFill.INSERT)
    /**
     * 插入时自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        String userInfo = "修改人";
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("createBy", userInfo, metaObject);
        this.setFieldValByName("updateBy", userInfo, metaObject);
        this.setFieldValByName("delFlag", 0, metaObject);
    }

    /**
     * 更新时自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        String userInfo = "修改人";
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("updateBy", userInfo, metaObject);
    }
}
